package com.atguigu.chapter06;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;
import java.util.Set;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/3/5 14:36
 */
public class Flink05_UV {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        // 1.读取数据：读文件
        SingleOutputStreamOperator<UserBehavior> userBehaviorDS = env
                .readTextFile("input/UserBehavior.csv")
                .map(new MapFunction<String, UserBehavior>() {
                    @Override
                    public UserBehavior map(String value) throws Exception {
                        String[] datas = value.split(",");
                        return new UserBehavior(
                                Long.valueOf(datas[0]),
                                Long.valueOf(datas[1]),
                                Integer.valueOf(datas[2]),
                                datas[3],
                                Long.valueOf(datas[4])
                        );
                    }
                });

        // 2.处理数据
        // 2.1 能过滤先过滤，过滤出PV行为
        SingleOutputStreamOperator<UserBehavior> pvDS = userBehaviorDS.filter(data -> "pv".equals(data.getBehavior()));
        // 2.2
        pvDS
                .map(new MapFunction<UserBehavior, Long>() {
                    @Override
                    public Long map(UserBehavior value) throws Exception {
                        return value.getUserId();
                    }
                })
                .process(new ProcessFunction<Long, Long>() {
                    private Set<Long> uvSet = new HashSet<>();

                    @Override
                    public void processElement(Long value, Context ctx, Collector<Long> out) throws Exception {
                        uvSet.add(value);
                        out.collect((long) uvSet.size());
                    }
                }).setParallelism(1)
                .print();


        // 去重？  按照什么去重？怎么去重？存哪里？
        //          按照 用户ID去重。
        //          Set结构：用户ID 添加进去，求Set的大小   (或 布隆过滤器)
        //          Redis、 程序本身


        env.execute();
    }
}
